#ifndef DHD_EVENT_NOTIFIER_H
#define DHD_EVENT_NOTIFIER_H


#include <boost/asio.hpp>
#include <boost/function.hpp>

namespace dhd
{
   
   typedef boost::function< void () > EventCallback;
   
   // This class integrates with the boost reactor loop and allows any thread
   // to 'notify()' the thread owning the EventNotifier that it needs to invoke
   // the EventCallback handler. Note that the 'notify()' and EventCallback invocations
   // are not necessarily 1-to-1. If multiple threads call 'notify()' in short order, there
   // may only be a single invocation of EventCallback (this is by design). 
   // 
   // Ideally, this will be paired with a mutex-protected event queue:
   //     def add_to_queue( event ):
   //         lock_mutex(q_mutex)
   //         q.push_back( event )
   //         unlock_mutex(q_mutex)
   //         q_notifier.notify()
   
   class EventNotifier
   {
       typedef boost::asio::local::stream_protocol::socket UnixSocket;
       
       char rbuff[20];
       
       UnixSocket writer;
       UnixSocket reader;
       
       EventCallback on_event;
       
       void handle_read( const boost::system::error_code & error, std::size_t size );
       
     public:
       
       EventNotifier( boost::asio::io_service & io_srv, EventCallback cb );
       
       void notify();
   };
   
}// end namespace dhd

#endif

